/**
 * 用户应用数据模板
 * @param dataContainerId 当前数据容器id
 */
system.userUseTemplate = function (dataContainerId) {
    var containerId = dataContainerId ? (dataContainerId.indexOf("#") == 0 ? dataContainerId : "#" + dataContainerId) : "#formContainer";
    layui.layer.open({
        type: 2,
        title: '模板应用',
        area: ['80%', '80%'],
        content: system.contextPath + '/common/dataTemplate/list',
        end: function () {
            var templateDataJson = top.window['templateDataJson'];
            if (!templateDataJson) {
                system.error("模板应用失败");
            } else {
                var tempContent = JSON.parse(templateDataJson.content);
                $(containerId).find("[name]").each(function () {
                    var $this = $(this);
                    var name = $this.attr("name");
                    var value = tempContent[name];
                    if (value) {
                        $this.val(value).text(value);
                    }
                });
                top.window['templateDataJson'] = null;
            }
        }
    });
}

/**
 * 应用数据模板
 * @param dataContainerId 当前数据容器id
 */
system.useTemplate = function (dataContainerId) {
    var containerId = dataContainerId ? (dataContainerId.indexOf("#") == 0 ? dataContainerId : "#" + dataContainerId) : "#formContainer";
    layui.layer.open({
        type: 2,
        title: '模板应用',
        area: ['80%', '80%'],
        content: system.contextPath + '/common/dataTemplate/list',
        end: function () {
            var templateDataJson = top.window['templateDataJson'];
            if (!templateDataJson) {
                system.error("模板应用失败");
            } else {
                $(containerId).find("[name]").each(function () {
                    var $this = $(this);
                    var name = $this.attr("name");
                    var value = templateDataJson[name];
                    if (value) {
                        $this.val(value).text(value);
                    }
                });
                top.window['templateDataJson'] = null;
            }
        }
    });
}

/**
 * 添加数据模板
 * @param dataContainerId 当前数据容器id
 */
system.buildTemplate = function (dataContainerId) {
    var containerId = dataContainerId ? (dataContainerId.indexOf("#") == 0 ? dataContainerId : "#" + dataContainerId) : "#formContainer";
    var templateData = {};
    $(containerId).find("[name]").each(function () {
        var $this = $(this);
        var value = $this.val() ? $this.val() : $this.text();
        templateData[$this.attr("name")] = value;
    });
    var templateDataJson = JSON.stringify(templateData);
    top.window['templateDataJson'] = templateDataJson;

    layui.layer.open({
        type: 2,
        title: '设置为模板',
        area: ['80%', '80%'],
        content: system.contextPath + '/common/dataTemplate/toBuildTemplate'
    });
}

system.uploadPath = function () {
    return '/resource/core/upload';
}
system.info = function (info) {
    top.layer.msg(info, {icon: 6});
};
system.success = function (info) {
    top.layer.msg(info, {icon: 1});
};
system.error = function (info) {
    top.layer.msg(info, {icon: 2});
};
system.confirm = function (tip, ensure) {
    top.layer.confirm(tip, {
        skin: 'layui-layer-admin'
    }, function () {
        ensure();
    });
};
system.copy = function copyContent(content) {
    var $input = $('<input readonly="readonly" value=\'' + content + '\'/>');
    $("body").append($input);
    $input[0].select();
    document.execCommand('copy') ? system.success("复制成功") : system.error("复制失败");
    $input.remove();
}
system.currentDate = function () {
    // 获取当前日期
    var date = new Date();

    // 获取当前月份
    var nowMonth = date.getMonth() + 1;

    // 获取当前是几号
    var strDate = date.getDate();

    // 添加分隔符“-”
    var seperator = "-";

    // 对月份进行处理，1-9月在前面添加一个“0”
    if (nowMonth >= 1 && nowMonth <= 9) {
        nowMonth = "0" + nowMonth;
    }

    // 对月份进行处理，1-9号在前面添加一个“0”
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }

    // 最后拼接字符串，得到一个格式为(yyyy-MM-dd)的日期
    return date.getFullYear() + seperator + nowMonth + seperator + strDate;
};
system.getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    } else {
        return null;
    }
};
system.infoDetail = function (title, info) {
    var display = "";
    if (typeof info === "string") {
        display = info;
    } else {
        if (info instanceof Array) {
            for (var x in info) {
                display = display + info[x] + "<br/>";
            }
        } else {
            display = info;
        }
    }
    top.layer.open({
        title: title,
        type: 1,
        skin: 'layui-layer-rim', //加上边框
        area: ['950px', '600px'], //宽高
        content: '<div style="padding: 20px;">' + display + '</div>'
    });
};
system.zTreeCheckedNodes = function (zTreeId) {
    var zTree = $.fn.zTree.getZTreeObj(zTreeId);
    var nodes = zTree.getCheckedNodes();
    var ids = "";
    for (var i = 0, l = nodes.length; i < l; i++) {
        ids += "," + nodes[i].id;
    }
    return ids.substring(1);
};

// 以下代码是配置layui扩展模块的目录，每个页面都需要引入
layui.config({
    base: system.contextPath + '/plugins/module/'
}).extend({
    formSelects: 'formSelects/formSelects-v4',
    treetable: 'treetable-lay/treetable',
    dropdown: 'dropdown/dropdown',
    notice: 'notice/notice',
    step: 'step-lay/step',
    dtree: 'dtree/dtree',
    citypicker: 'city-picker/city-picker',
    tableSelect: 'tableSelect/tableSelect',
    ax: 'ax/ax',
    ztree: 'ztree/ztree-object'
}).use(['admin'], function () {
    var $ = layui.$;
    var admin = layui.admin;

    // 单标签模式需要根据子页面的地址联动侧边栏的选中，用于适配浏览器前进后退按钮
    if (window != top && top.layui && top.layui.index && !top.layui.index.pageTabs) {
        top.layui.admin.activeNav(location.href.substring(system.contextPath.length));
    }

    // 移除loading动画
    setTimeout(function () {
        admin.removeLoading();
    }, window == top ? 300 : 150);

    //注册session超时的操作
    $.ajaxSetup({
        contentType: "application/x-www-form-urlencoded;charset=utf-8",
        complete: function (XMLHttpRequest, textStatus) {

            //通过XMLHttpRequest取得响应头，sessionstatus，
            var sessionstatus = XMLHttpRequest.getResponseHeader("sessionstatus");
            if (sessionstatus === "timeout") {

                //如果超时就处理 ，指定要跳转的页面
                window.location = system.contextPath + "/global/sessionError";
            }
        }
    });

});